﻿
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace nwrole.Module.lib
{
    public class ThreadTraceEventArgs : EventArgs
    {
        public string Message;
        public string Source;
        public TraceEventCache EventCache;
        public TraceEventType EventType;
    }

    public class ThreadTraceListener : TraceListener
    {
        public delegate void ThreadTraceHandler(object sender, ThreadTraceEventArgs e);

        public event ThreadTraceHandler OnTrace;

        public override void Write(string message)
        {
        }

        public override void WriteLine(string message)
        {
        }

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id)
        {
            base.TraceEvent(eventCache, source, eventType, id);
        }

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
        {
            ThreadTraceEventArgs eventArgs = new ThreadTraceEventArgs();
            eventArgs.EventCache = eventCache;
            eventArgs.Source = source;
            eventArgs.EventType = eventType;
            eventArgs.Message = String.Format(format, args);

            if (OnTrace != null)
                OnTrace(this, eventArgs);

            WriteLine(eventArgs.Message);

            //base.TraceEvent(eventCache, source, eventType, id, format, args);
        }

        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
        {
            ThreadTraceEventArgs args = new ThreadTraceEventArgs();
            args.EventCache = eventCache;
            args.Source = source;
            args.EventType = eventType;
            args.Message = message;
            
            if (OnTrace != null)
                OnTrace(this, args);

            WriteLine(message);

            //base.TraceEvent(eventCache, source, eventType, id, message);
        }
    }
}
